package com.ccys.cms.controller.consultation;


import cn.hutool.core.util.ObjectUtil;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.consultation.DeliveryInfoUpdateDto;
import com.ccys.db.entity.TblBookingDeliveryAddress;
import com.ccys.db.entity.TblBookingDeliveryLocation;
import com.ccys.db.mapper.TblBookingConsultNoteMapper;
import com.ccys.db.mapper.TblBookingDeliveryAddressMapper;
import com.ccys.db.mapper.TblBookingDeliveryLocationMapper;
import com.ccys.db.vo.consultation.ConsultationDetailDeliveryInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "问诊订单列表详情Delivery Info")
@RequestMapping("consultation")
public class ConsultationDetailDeliveryInfoController {


    @Resource
    private TblBookingConsultNoteMapper consultNoteMapper;

    /**
     * time based
     */
    @Resource
    private TblBookingDeliveryAddressMapper addressMapper;

    /**
     * localtion based
     */
    @Resource
    private TblBookingDeliveryLocationMapper locationMapper;


    @GetMapping("getDeliveryInfo")
    @ApiOperation(value = "查询deliveryInfo详情")
    public ResponseResult<List<ConsultationDetailDeliveryInfoVo>, Object> getDeliveryInfo(String bookingId) {
        //通过bookingId找到patient的国家
        //在tbl_delivery_country表里面找到国家快递设置的类型 time based  / location based (没有默认展示location based?)
        //根据快递设置的类型查询/更新数据
        //deliveryFee在tbl_booking_fee里面
        //（timebased才有）deliveryTime ->deliveryAddress 里面 patientSelectedAt （这是utc 时间 要转成当地时间 ） localtionbased 展示当地的时区？
        Integer deliveryType = consultNoteMapper.getDeliveryType(bookingId);
        List<ConsultationDetailDeliveryInfoVo> info = new ArrayList<>();
        //快递类型:1:location-based;2:time-based
        if (ObjectUtil.isNotNull(deliveryType)) {
            if (1 == deliveryType) {
                info = consultNoteMapper.getLocationBasedDeliveryInfo(bookingId);

            } else if (2 == deliveryType) {
                info = consultNoteMapper.getTimeBasedDeliveryInfo(bookingId);
            }

        }
        return ResponseResult.success(info);
    }

    @GetMapping("getReactivatedDeliveryInfo")
    @ApiOperation(value = "查询Reactivated deliveryInfo详情",notes = "传当前的booking就行")
    public ResponseResult<List<ConsultationDetailDeliveryInfoVo>, Object> getReactivatedDeliveryInfo(String bookingId) {

        // 2024/1/2 0002 最多复购一次
        List<ConsultationDetailDeliveryInfoVo> infoList = new ArrayList<>();
        Integer deliveryType = consultNoteMapper.getDeliveryType(bookingId);
        //快递类型:1:location-based;2:time-based
        String reactiveOrderId = consultNoteMapper.getReactiveOrderId(bookingId);
        if (1 == deliveryType) {
            //最多有1次复购
            infoList = consultNoteMapper.getLocationBasedDeliveryInfo(reactiveOrderId);

        } else if (2 == deliveryType) {
            //最多有1次复购
            infoList = consultNoteMapper.getTimeBasedDeliveryInfo(reactiveOrderId);

        }
        return ResponseResult.success(infoList);
    }


    @PostMapping("updateDeliveryInfo")
    @ApiOperation(value = "更新deliveryInfo", notes = "老的 delivery info 数据的 id 要传过来，Unit number 不明确 ")
    @Transactional
    public ResponseResult updateDeliveryInfo(DeliveryInfoUpdateDto model) {
        String bookingId = model.getBookingId();
        Integer deliveryType = consultNoteMapper.getDeliveryType(bookingId);
        //快递类型:1:location-based;2:time-based
        if (1 == deliveryType) {
            String oldId = model.getOldId();
            TblBookingDeliveryLocation oldLocation = new TblBookingDeliveryLocation();
            oldLocation.setId(oldId);
            oldLocation.setStatus(6);
            locationMapper.updateByPrimaryKeySelective(oldLocation);

            TblBookingDeliveryLocation location = new TblBookingDeliveryLocation();
            BeanUtils.copyProperties(model, location);
            location.setStatus(2);
            locationMapper.insert(location);

        } else if (2 == deliveryType) {

            String oldId = model.getOldId();
            //上一条数据的status数据修改成6 新插入的数据status为2
            TblBookingDeliveryAddress oldAddress = new TblBookingDeliveryAddress();
            oldAddress.setId(oldId);
            oldAddress.setStatus(6);
            addressMapper.updateByPrimaryKeySelective(oldAddress);

            TblBookingDeliveryAddress address = new TblBookingDeliveryAddress();
            BeanUtils.copyProperties(model, address);
            address.setStatus(2);
            addressMapper.insert(address);

        }
        return ResponseResult.success();
    }


}
